package net.oschina.app.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.zxing.activity.CaptureActivity;

import net.oschina.app.R;
import net.oschina.app.interf.FragmentType;
import net.oschina.app.ui.ExploreActivityActivity;
import net.oschina.app.ui.RvFragmentActivity;
import net.oschina.app.ui.ShakeActivity;
import net.oschina.app.util.CommonUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * Created by wei03 on 2017/1/12.
 * 发现界面
 */
public class ExploreFragment extends Fragment {
    @BindView(R.id.ll_friend)
    LinearLayout mLlFriend;
    @BindView(R.id.ll_Looking)
    LinearLayout mLlLooking;
    @BindView(R.id.ll_activity)
    LinearLayout mLlActivity;
    @BindView(R.id.ll_scanning)
    LinearLayout mLlScanning;
    @BindView(R.id.ll_shake)
    LinearLayout mLlShake;
    //二维码扫描
    public static final int REQUEST_CODE = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = CommonUtils.inflate(R.layout.fragment_discover);
        ButterKnife.bind(this, view);
        return view;
    }

    @OnClick({R.id.ll_friend, R.id.ll_Looking, R.id.ll_activity, R.id.ll_scanning, R.id.ll_shake})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_friend:
                Intent intent = new Intent(getContext(), RvFragmentActivity.class);
                intent.putExtra(FragmentType.FRAGMENT_TYPE, FragmentType.TYPE_EXPLORE_FRIENDS);
                startActivity(intent);
                break;
            case R.id.ll_Looking:
                break;
            case R.id.ll_activity:
                startActivity(new Intent(getContext(),ExploreActivityActivity.class));
                break;
            case R.id.ll_scanning:
                scanning();
                break;
            case R.id.ll_shake:
                enterShake();
                break;
        }
    }

    //进入摇一摇
    private void enterShake() {
//        Intent intent = new Intent(getContext(), ShakeActivity.class) ;
        Intent intent = new Intent(getContext(), ShakeActivity.class) ;
        startActivity(intent);
    }

    private void scanning() {
        //扫描操作
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Toast.makeText(getActivity(), scanResult, Toast.LENGTH_LONG).show();

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(scanResult));
            startActivity(intent);
        }
    }
}
